package getCourseM3U8;

import java.io.File;

/**
 * 本方法用于将文件序号格式化为定长的，前面用0填充
 * @author Administrator
 *
 */
public class orderRename {
	//序号长度
	private static final int order_length = 3;
	private static final String order_format = "%0" + order_length + "d";
	
	/**
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void renameFile(File[] files) throws Exception{
		for(File f:files) {
			//一并搜寻目录下的文件
			if(f.isDirectory()) {
				renameFile(f.listFiles());
			}
			
			if(!f.getName().contains(".m3u8")) continue;
			//找到当前目录里的m3u8文件
			StringBuilder sb = new StringBuilder(f.getName()); 
			int space_idx = sb.indexOf(" ");
			Integer order = Integer.parseInt(sb.substring(0, space_idx));
			sb.delete(0, space_idx);
			
			//取得文件所在目录
			//System.out.println(f.getParent());
			File newName = new File(f.getParent() + "\\" + String.format(order_format, order) + sb.toString());			
			f.renameTo(newName);
			
		}
	}

	public static void main(String[] args) throws Exception{
		// TODO 自动生成的方法存根
		File root_dir = new File("考研视频流所在路径");
		File[] filesToRename= root_dir.listFiles();
		renameFile(filesToRename);
	}

}
